package com.xms.queryview.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.dependency.XmsDependencyException;
import com.xms.queryview.entity.QueryView;
import com.xms.schema.event.EntityMetaDeletedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

/**
 * <p>
 * 实体删除后，级联删除视图
 * </p>
 *
 * @author migo
 * @since 2021/10/26
 */
@Service
public class QueryViewCascadeDeleter {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private IQueryViewDeleterService _queryViewDeleterService;

    @Autowired
    public void QueryViewCascadeDeleter(IQueryViewDeleterService queryViewDeleterService) {
        _queryViewDeleterService = queryViewDeleterService;
    }

    @Async
    @EventListener()
    public void onApplicationEvent(EntityMetaDeletedEvent event) throws XmsDependencyException, SQLException {
        logger.info("QueryViewCascadeDeleting");
        _queryViewDeleterService.deleteMany(new LambdaQueryWrapper<QueryView>().eq(QueryView::getEntityid, event.source.getEntityid()));
    }
}
